package com.yuanchu.marketing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.common.exception.YuanChuException;
import com.yuanchu.common.utils.DateFormatUtils;
import com.yuanchu.common.utils.PageResult;
import com.yuanchu.marketing.dao.YoshopRechargePlanDao;
import com.yuanchu.marketing.dto.RechargePlanParamsDto;
import com.yuanchu.marketing.entity.YoshopRechargePlanEntity;
import com.yuanchu.marketing.service.YoshopRechargePlanService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.Instant;


@Service("yoshopRechargePlanService")
public class YoshopRechargePlanServiceImpl extends ServiceImpl<YoshopRechargePlanDao, YoshopRechargePlanEntity> implements YoshopRechargePlanService {


    @Override
    public PageResult<YoshopRechargePlanEntity> queryRechargePlanList(Integer page, String search) {
        LambdaQueryWrapper<YoshopRechargePlanEntity> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(search), YoshopRechargePlanEntity::getPlanName, search);
        lqw.orderByDesc(YoshopRechargePlanEntity::getCreateTime);
        Page<YoshopRechargePlanEntity> planEntityPage = this.page(new Page<>(page, 15), lqw);
        planEntityPage.getRecords().forEach(item -> {
            item.setCreateTime(DateFormatUtils.formatDatetime(item.getCreateTime()));
        });
        return PageResult.formatPageResult(planEntityPage.getRecords(), page, 15, planEntityPage.getTotal());
    }

    @Override
    public void addRechargePlan(RechargePlanParamsDto params) {
        Integer planId = params.getPlanId();
        YoshopRechargePlanEntity planEntity = this.getOne(new LambdaQueryWrapper<YoshopRechargePlanEntity>()
                .eq(YoshopRechargePlanEntity::getPlanId, planId));
        if (planEntity != null){
            return;
        }
        planEntity = new YoshopRechargePlanEntity();
        BeanUtils.copyProperties(params.getForm(), planEntity);
        planEntity.setCreateTime(String.valueOf(Instant.now().toEpochMilli()/1000));
        planEntity.setUpdateTime(String.valueOf(Instant.now().toEpochMilli()/1000));
        planEntity.setStoreId(10001);
        boolean save = this.save(planEntity);
        if (!save){
            throw new YuanChuException("添加失败");
        }
    }

    @Override
    public void editRechargePlan(RechargePlanParamsDto params) {
        Integer planId = params.getPlanId();
        YoshopRechargePlanEntity planEntity = this.getOne(new LambdaQueryWrapper<YoshopRechargePlanEntity>()
                .eq(YoshopRechargePlanEntity::getPlanId, planId));
        if (planEntity == null){
            throw new YuanChuException("套餐不存在");
        }
        YoshopRechargePlanEntity form = params.getForm();
        BeanUtils.copyProperties(form, planEntity);
        planEntity.setPlanId(planId);
        planEntity.setUpdateTime(String.valueOf(Instant.now().toEpochMilli()/1000));
        boolean save = this.updateById(planEntity);
        if (!save){
            throw new YuanChuException("修改失败");
        }
    }

    @Override
    public void deleteRechargePlan(Integer planId) {
        boolean update = this.update(new UpdateWrapper<YoshopRechargePlanEntity>()
                .eq("plan_id", planId)
                .set("is_delete", 1)
        );
        if (!update){
            throw new YuanChuException("删除失败");
        }
    }
}